<?php
namespace app\admin\controller;
use think\Db;
use app\common\validate\BannerDetailValidate;
use app\common\validate\BannerValidate;
use app\admin\model\BannerDetail;
use app\admin\model\Banner as BannerModel;
use think\facade\Cache;
class Banner extends Common {
	public function __construct(){
		parent::__construct();
	}
	// 友情链接首页
	public function index(){
		return $this->fetch();
	}
	public function index_lists(){
		$limit = request()->param('limit');
		$page = request()->param('page');
		$start = ($page-1)*$limit;
		$count = db('banner')->count();
		$result = db('banner')->limit($start,$limit)->order('id desc,sort desc')->select();
		
		return showTable(0, 'OK', $count ,$result, 200);
		
	}
	
	public function banner_lists(){
		$limit = request()->param('limit');
		$page = request()->param('page');
		$start = ($page-1)*$limit;
		$count = db('banner_detail')->count();
		$result= db('banner_detail')
				->alias('a')
				->join('banner b','a.type = b.id')
				->limit($start,$limit)
				->order('a.sort desc,a.id desc')
				->field('a.id,title,url,logo,is_check,sort,a.type as type,b.name')
				->cache('banner_detail'.$limit.$page,3600,'banner_detail')
				->select();
		return showTable(0, 'OK', $count ,$result, 200);
	}
	// 友情链接类别
	public function typelists(){
		return $this->fetch();
	}
	// 友情链接类别数据
	public function typelists_data(){
		
		$type= db('banner_detail')->column('type');
		$limit = request()->param('limit');
		$page = request()->param('page');
		$start = ($page-1)*$limit;
		$count = db('banner')->count();
		$result = db('banner')->limit($start,$limit)->order('lsort asc,id desc')->cache('banner'.$limit.$page,3600,'banner')->select();
		foreach($result as $k=>$v){
			$num = 1;
			foreach($type as $kk=>$vv){
				if($v['id'] == $vv){
					$result[$k]['count'] = $num++;
				}
			}
		}
		return showTable(0, 'OK', $count ,$result, 200);
	}
	// 友情链接类别添加
	public function typeadd(){
		
		if(request()->isPost()){
			$data = request()->param();
			$validate = new BannerValidate;
			if (!$validate->scene('add')->check($data)) {
				return show(0, $validate->getError(), 0 , 200);
			}else{
				Cache::clear('banner'); 
				$rest = (new BannerModel())->strict(false)->insert($data);
				return show(1, lang('add success'), $rest, 200);
			}
		}
		return $this->fetch();
	}
	//友情链添加
	public function banneradd(){
		if(request()->isPost()){
			$data = request()->param();
			$data['post_time'] = date('Y-m-d h:i:s', time());		
			$validate = new BannerDetailValidate;
			if (!$validate->scene('add')->check($data)) {
				return show(0, $validate->getError(), 0 , 200);
			}else{
				Cache::clear('banner_detail');
				$rest = (new BannerDetail())->strict(false)->insert($data);
				return show(1, '添加成功', $rest, 200);
			}			
		}
		$result = db('banner')->field('name,id')->select();
		$this->assign('type',$result);
		return $this->fetch();
	}
	//友情链接排序
	public function bannersort(){
		$data = request()->param();
		
		$validate = new BannerDetailValidate;
		
		if (!$validate->scene('sort')->check($data)) {
			return show(config('code.error'), 'error', $validate->getError(), 200);
		}else{
			Cache::clear('banner_detail');
			$rest = (new BannerDetail())->save(['sort'=>$data['sort']],['id'=>$data['id']]);
			return show(config('code.success'), 'OK', $rest, 200);
		}
			
	}
	//友情链接删除
	public function bannerdele(){
		Cache::clear('banner_detail');
		$data = request()->param();
		$result = db('banner_detail')->delete($data['id']);
		return show(1, '删除成功', $result, 200);
	}
	//友情链接全删
	public function banner_all_dele(){
		Cache::clear('banner_detail');
		$data = request()->param();
		$result = db('banner_detail')->delete($data['ids']);
		return show(1, '删除成功', $result, 200);
	}
	//友情链接是否显示
	public function banner_check(){
		$data = request()->param();
		Cache::clear('banner_detail');
		$result = db('banner_detail')->where('id',$data['id'])->update(['is_check'=>$data['status']]);
		return show(1, '设置成功', $result, 200);
	}
	//友情链接编辑
	public function banneredit(){
		if(request()->isPost()){
			$data = request()->param();
			$data['post_time'] = date('Y-m-d h:i:s', time());		
			$validate = new BannerDetailValidate;
			if (!$validate->scene('edit')->check($data)) {
				return show(0, $validate->getError(), 0 , 200);
			}else{
				Cache::clear('banner_detail');
				$rest = (new BannerDetail())->strict(false)->where('id',$data['id'])->update($data);
				return show(1, '修改成功', $rest, 200);
			}			
		}
		$result = db('banner')->field('name,id')->select();
		$this->assign('type',$result);
		return $this->fetch();
	}
	//友情链接编辑读取当前item
	public function banner_edit(){
		$data = request()->param();
		$result = db('banner_detail')->where('id',$data['id'])->find();
		return show(1, '获取成功', $result, 200);
	}
	//友情链接类别单条删除
	public function banner_type_del(){
		$data = request()->param();
		$rest = db('banner_detail')->where('type',$data['id'])->find();
		if($rest){
			return show(0, '该类别底下有数据，请删除数据或者移动到别的类别下再进行操作！', "{}", 200);
		}else{
			Cache::clear('banner');
			$result = db('banner')->delete($data['id']);
			return show(1, '删除成功', $result, 200);
		}
		
	}
	//友情链接类别多条删除
	public function banner_type_alldel(){
		$data = request()->param();
		$ndata = array_flip(array_flip($data['ids']));
		foreach($ndata as $k=>$v){
			$rest = db('banner_detail')->where('type',$v)->find();
			if($rest){
				return show(0, '选择的条目底下有数据，请删除数据或者移动到别的类别下再进行操作！', "{}", 200);
			}
		}
		Cache::clear('banner');
		$result = db('banner')->delete($data['ids']);
		return show(1, '删除成功', $result, 200);
	}
	//友情链接类别编辑页面
	public function typeedit(){
		return $this->fetch();
	}
	//友情链接类别编辑数据获取
	public function banner_type_edit(){
		if(request()->isPost()){
			$data = request()->param();	
			$validate = new BannerValidate;
			if (!$validate->scene('edit')->check($data)) {
				return show(0, $validate->getError(), 0 , 200);
			}else{
				Cache::clear('banner');
				Cache::clear('banner_detail');
				$rest = (new BannerModel())->strict(false)->where('id',$data['id'])->update($data);
				return show(1, '修改成功', $rest, 200);
			}			
		}
		$data = request()->param();
		$result = db('banner')->where('id',$data['id'])->find();
		return show(1, '获取成功', $result, 200);
	}
	 
	public function typesort(){
		if(request()->isPost()){
			$data = request()->param();
			
			$validate = new BannerValidate;
			
			if (!$validate->scene('sort')->check($data)) {
				return show(config('code.error'), 'error', $validate->getError(), 200);
			}else{
				Cache::clear('banner');
				$rest = (new BannerModel())->save(['lsort'=>$data['lsort']],['id'=>$data['id']]);
				return show(config('code.success'), 'OK', $rest, 200);
			}
		}
	}
}
